package com.csi.jf.crucible.ptservice.controller;

import java.io.File;
import java.sql.Timestamp;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.chinasofti.ro.bizframework.core.data.validation.Validation;
import com.chinasofti.ro.bizframework.core.json.jsonlib.processor.TimestampJsonValueProcessor;
import com.chinasofti.ro.bizframework.core.mvc.Controller;
import com.chinasofti.ro.bizframework.core.mvc.View;
import com.chinasofti.ro.bizframework.core.orm.Page;
import com.chinasofti.ro.bizframework.core.orm.PageUtil;
import com.chinasofti.ro.bizframework.core.orm.entity.Model;
import com.chinasofti.ro.bizframework.modules.json.JSONArray;
import com.chinasofti.ro.bizframework.modules.json.JsonConfig;
import com.csi.jf.crucible.common.util.LoginUtils;
import com.csi.jf.crucible.common.util.PackageUtils;
import com.csi.jf.crucible.ptservice.model.CodePackages;
import com.csi.jf.crucible.ptservice.model.ProjectTemp;
import com.csi.jf.crucible.ptservice.service.CodePackagesService;

/**
 * <p>
 * Controller
 * </p>
 * 
 * @author BizFoundation
 * @version 1.0
 * @since 1.0
 */
public class CodePackagesController extends Controller {

    protected static final Logger LOG = LoggerFactory
            .getLogger(CodePackagesController.class);

    /**
     * <p>
     * 打开查询列表页面
     * </p>
     * 
     */
    public void list() {
        View view = new View(SUCCESS);
        this.render(view);
    }

    /**
     * <p>
     * 查询分页数据
     * </p>
     * 
     * @param gridPage
     * @param codePackages
     */
    public void listCodePackages(Page page, CodePackages codePackages) {
        // 自动加入当前项目进行过滤
        Long projectId = LoginUtils.getCurrentProject().getProjectId();
        if (null != projectId) {
            codePackages.setProjectId(projectId);
        }
        page = codePackagesService.findByPage(codePackages, page);
        this.renderJSON(PageUtil.toJson4JqGrid(page, true));
    }

    public void show(Long id) {
        if (null == id) {
            throw new NullPointerException("主键为Null");
        }
        Model codePackages = codePackagesService.findById(id);
        View view = new View(SUCCESS);
        view.bind("codePackages", codePackages);
        this.render(view);
    }

    public void add() {
        this.render(SUCCESS);
    }

    public void save(CodePackages codePackages, File packageFile, String from) {
        View view = null;
        if (Validation.hasErrors()) {
            view = new View(FAILED);
            view.bind("codePackages", codePackages);
            this.render(view);
            return;
        }
        // copy file from temp-upload to package directory
        String packagename = this.getOldOriginalFileName("packageFile");
        String filename = packagename
                .substring(0, packagename.lastIndexOf("."));
        codePackages.setPackagename(filename);
        codePackages.setExtendName(packagename.substring(
                packagename.lastIndexOf("."), packagename.length()));

        try {
            codePackages.setCreator(LoginUtils.getLoginUser().getJfid());
            codePackages
                    .setCreatetime(new Timestamp(System.currentTimeMillis()));
            codePackages.setClassPaths(packageFile.toString());
            codePackages.setUpdater(0L);
            codePackages.setUpdatetime(codePackages.getCreatetime());
            // upload file
            PackageUtils.uploadPackage(codePackages, packageFile);
        } catch (Exception e) {
            LOG.error(e.getMessage(), e);
            view = new View(FAILED);
            view.bind("codePackages", codePackages);
            this.render(view);
            return;
        }

        codePackagesService.save(codePackages);
        if (from != null && "tool".equals(from)) {
            view = new View("tool");
            this.success("add.success");
            this.render(view);
        } else {
            this.renderJSON("{\"result\":\"success\"}");
        }
    }

    public void edit(Long id) {
        if (null == id) {
            throw new NullPointerException("主键为Null");
        }
        Model codePackages = codePackagesService.findById(id);
        View view = new View(SUCCESS);
        view.bind("codePackages", codePackages);
        this.render(view);
    }

    public void update(CodePackages codePackages) {
        codePackagesService.update(codePackages);
        View view = new View(SUCCESS);
        this.success("update.success");
        this.redirect(view);
    }

    public void delete(String ids) {
        this.codePackagesService.batchDelete(ids);
        View view = new View(SUCCESS);
        this.success("delete.success");
        this.redirect(view);
    }

    /**
     * 获取当前登录人的项目信息
     * 
     * @author JCJ
     */
    public void getProjectInfo() {
        String manager = null;
        if (null != LoginUtils.getLoginUser() && null != LoginUtils.getLoginUser().getJfid()) {
            manager = LoginUtils.getLoginUser().getJfid().toString();
        }
        List<ProjectTemp> projectList = codePackagesService.getProjectInfo(manager);
        JsonConfig config = new JsonConfig();
        config.registerJsonValueProcessor(java.sql.Timestamp.class,new TimestampJsonValueProcessor());
        String json = JSONArray.fromObject(projectList, config).toString();
        this.renderJSON(json);
    }

    /**
     * 根据项目ID获取项目信息
     * 
     * @author zc
     */
    public void getProInfoByID(Long proID) {
        ProjectTemp projec = codePackagesService.getProjectInfoByID(proID);
        if (projec != null)
            this.renderJSON(projec.toJson());
    }

    /**
     * <p>
     * 注入 codePackagesService
     * </p>
     */
    private CodePackagesService codePackagesService;

    public void setCodePackagesService(CodePackagesService codePackagesService) {
        this.codePackagesService = codePackagesService;
    }
}